﻿using H.Logger.Logs.Listeners;
using H.Logs;
using System;

namespace H.Logger.Logs
{
    /// <summary>
    /// 日志收集器工厂
    /// </summary>
    public class HLoggerFactory : ILoggerFactory
    {
        /// <summary>
        /// 缓存已经创建的日志收集器
        /// </summary>
        private static WeakReference _Instance = null;

        ///<inheritdoc/>
        public ILogCollector GetInstance()
        {
            if (_Instance == null || !_Instance.IsAlive)
            {
                _Instance = new WeakReference(new HLogCollector());
            }
            return _Instance.Target as ILogCollector;
        }


        ///<inheritdoc/>
        public ILogListener CreateWebLogListener(string hostName, int port, string clientName)
        {
            return new HWebLogListener(hostName, port, clientName);
        }

        ///<inheritdoc/>
        public ILogListener CreateLocalLogListener()
        {
            return HLocalLogListener.GetInstance();
        }

        ///<inheritdoc/>
        public ILogListener CreateFileLogListener(string filePrefix)
        {
            return new HFileLogListener(HService.GetEnvPath("log"), filePrefix);
        }
    }
}
